//
//  WKSWebView.m
//  HaiGeEr_Student
//
//  Created by liuchen on 2018/9/10.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "WKSWebView.h"
@interface WKSWebView()
@property (nonatomic, weak) WKWebView *wkWebView;
@property (nonatomic, weak) UIProgressView *progressView;
@end

@implementation WKSWebView

- (instancetype)initWithFrame:(CGRect)frame WithUrl:(NSURL *)url{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewWithUrl:url];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubViewWithUrl:(NSURL *)url{
    //进度条
    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 3*SCALEUNIT)];
    progressView.backgroundColor = [UIColor blueColor];
    //设置进度条的高度，下面这句代码表示进度条的宽度变为原来的1倍，高度变为原来的1.5倍.
    progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    _progressView = progressView;
    [self addSubview:self.progressView];
    //webView
    WKWebView *wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.progressView.frame),  self.frame.size.width, self.frame.size.height-CGRectGetMaxY(self.progressView.frame))];
    wkWebView.UIDelegate = self;
    wkWebView.navigationDelegate = self;
    _wkWebView = wkWebView;
    [self.wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    [self addSubview:self.wkWebView];
    [self.wkWebView loadRequest:[NSURLRequest requestWithURL:url]];
    //[self.wkWebView loadHTMLString:[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"UserProtocol" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil] baseURL:nil];
}

#pragma mark ==========观察者==========
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        self.progressView.progress = self.wkWebView.estimatedProgress;
        if (self.progressView.progress == 1) {
            /*
             *添加一个简单的动画，将progressView的Height变为1.4倍，在开始加载网页的代理中会恢复为1.5倍
             *动画时长0.25s，延时0.3s后开始动画
             *动画结束后将progressView隐藏
             */
            __weak typeof (self)weakSelf = self;
            [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
            } completion:^(BOOL finished) {
                weakSelf.progressView.hidden = YES;
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    //开始加载网页时展示出progressView
    self.progressView.hidden = NO;
    //开始加载网页的时候将progressView的Height恢复为1.5倍
    self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
    //防止progressView被网页挡住
    [self bringSubviewToFront:self.progressView];
}

//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    //加载完成后隐藏progressView
    self.progressView.hidden = YES;
}

//加载失败
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    //加载失败同样需要隐藏progressView
    self.progressView.hidden = YES;
}

- (void)dealloc {
    [self.wkWebView removeObserver:self forKeyPath:@"estimatedProgress"];
}




@end
